package controller;


import com.alibaba.fastjson2.JSONObject;
import com.atguigu.admin.bean.ActivityInformation;
import com.atguigu.admin.bean.CityPrize;
import com.atguigu.admin.service.ActivityInformationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;

@RestController
@RequestMapping("/act")
@RequiredArgsConstructor
public class ActivityInformationController {

    private final ActivityInformationService actService;

    private final RestTemplate restTemplate;

    private final static String URL = "https://apis.map.qq.com/ws/location/v1/ip";
    private final static String KEY = "XY6BZ-O75EW-AF5R6-YW6UK-J52NK-KOBKP";


    @PostMapping("/publish")
    public String  publish(@RequestBody ActivityInformation act){
        boolean save = actService.save(act);
        if(save) return "发布成功";
        return "失败";
    }

    @PostMapping("/grant")
    public String grant(@RequestBody CityPrize cityPrize){

        return "";
    }




    @GetMapping("/pos")
    public JSONObject getPos() throws URISyntaxException {
        ResponseEntity<JSONObject> entity = restTemplate.getForEntity(new URI(URL + "?key=" + KEY), JSONObject.class);
        System.out.println(URL);
        return entity.getBody();
    }

}
